﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Collections.ObjectModel;
using OntologyCache;
using System.Collections;

namespace biolit_config
{

    public class OntList
    {
        public OntList()
        {

        }

        public OntList(string name, string id)
        {
            Name = name;
            ID = id;

        }

        public OntList(string name, string id, string downloadURL)
        {
            Name = name;
            ID = id;
            DownloadURL = downloadURL;
        }

        public OntList(string name, string id, string version, string format, string source)
        {
            Name = name;
            ID = id;
            Version = version;
            Format = format;
            Source = source;
        }
        public string Version { get; set; }
        public string Format { get; set; }
        public string Source { get; set; }
        public string name = string.Empty;
        public string id = string.Empty;
        public string downloadURL = string.Empty;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string ID
        {
            get { return id; }
            set { id = value; }
        }

        public string DownloadURL
        {
            get { return downloadURL; }
            set { downloadURL = value; }
        }

        public static void Remove(OntList aWidget)
        {
            if (Biolit_Settings.Settings.AvailableOntologies != null)
            {
                List<OntList> oList = new List<OntList>();
                oList = Biolit_Settings.Settings.AvailableOntologies.ToList();
                for (int i = 0; i < oList.Count; i++)
                {
                    if (aWidget.ID == oList[i].ID)
                        oList.Remove(aWidget);
                }
                Biolit_Settings.Settings = new ConfigSettings();
                Biolit_Settings.Settings.AvailableOntologies = oList.ToArray();
                Biolit_Settings.StoreSettings();
            }
        }
        public bool IsMatch(OntList objOntList)
        {
            return objOntList.ID ==  this.ID && objOntList.Name.ToLower() == this.Name.ToLower();
        }

    }
    public class DownloadHistory
    {
        
        public string _OntologyID { get; set; }
        public string _OntologyName { get; set; }
        public string _Version { get; set; }
        public string _Format { get; set; }
        public string _Status { get; set; }
        public string _DateTime { get; set; }
        public string _FileSize { get; set; }
        public string _Error { get; set; }
        public string _Source { get; set; }
        public DownloadHistory(string OntologyID, string OntologyName, string Version, string Format, string Status, string DateTime, string FileSize, string Error, string Source)
        {
            this._OntologyID = OntologyID;
            this._OntologyName = OntologyName;
            this._Version = Version;
            this._Format = Format;
            this._Status = Status;
            this._DateTime = DateTime;
            this._FileSize = FileSize;
            this._Error = Error;
            this._Source = Source;
        }
        public DownloadHistory()
        {

        }

    }

    public class OntologyDownloadLocation
    {
        public OntologyDownloadLocation()
        {

        }
        public OntologyDownloadLocation(string Name, string URL)
        {
            name = Name;
            url = URL;
        }
        private string name;
        private string url;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string URL
        {
            get { return url; }
            set { url = value; }
        }
    }

    public class ConfigSettings
    {
        private double settingsVersion;
        public double SettingsVersion
        {
            get { return settingsVersion; }
            set { settingsVersion = value; }
        }

        private double currentSettingsVersion;
        public double CurrentSettingsVersion
        {
            get { return 1.1; } // increment when making additions to settings
        }

        private string obodwnldpath;
        public string OBO_DWNLDS
        {
            get { return obodwnldpath; }
            set { obodwnldpath = value; }
        }

        public string MainURL { get; set;}
        private OntologyDownloadLocation selectedDownloadURL;
        public OntologyDownloadLocation SelectedDownloadURL
        {
            get { return selectedDownloadURL; }
            set { selectedDownloadURL = value; }
        }

        public OntologyDownloadLocation[] AvailableDownloadURLs
        {
            get;
            set;
        }
        private OntList[] availOnt;
        public OntList[] AvailableOntologies
        {
            get { return availOnt; }
            set { availOnt = value; }

        }
        public OntList[] failedOnt { get; set; }
        private OntList[] listedOnt;
        public OntList[] ListedOntologies
        {
            get { return listedOnt; }
            set { listedOnt = value; }

        }
        public DownloadHistory[] DownloadedOntologies { get; set; }
        bool protdatbank;
        public bool ProteinDataBank
        {
            get { return protdatbank; }
            set { protdatbank = value; }
        }

        bool uniprot;
        public bool UniProt
        {
            get { return uniprot; }
            set { uniprot = value; }
        }
        public bool ontologiesUpdate { get; set;}
        public int downloadsSettings { get; set; }
        bool ncbi;
        public bool NCBI
        {
            get { return ncbi; }
            set { ncbi = value; }
        }

        bool autoStart;
        public bool AutoStart
        {
            get { return autoStart; }
            set { autoStart = value; }
        }

        bool inlinematching;
        public bool InlineMatching
        {
            get { return inlinematching; }
            set { inlinematching = value; }
        }

        bool alwaysoffline;
        public bool AlwaysOffline
        {
            get { return alwaysoffline; }
            set { alwaysoffline = value; }
        }

        bool commontermsmatch;
        public bool NoMatchCommonTerms
        {
            get { return commontermsmatch; }
            set { commontermsmatch = value; }
        }

        private OntologyDownloadLocation[] downloadURLLocations;
        public OntologyDownloadLocation[] DownloadURLLocations
        {
            get { return downloadURLLocations; }
            set { downloadURLLocations = value; }
        }

        public string FindOntologyID(string OntologyName)
        {
            string OntologyID = null;
            int Len = this.AvailableOntologies.Length;
            for (int j = 0; j < Len; ++j)
            {
                if (AvailableOntologies[j].Name == OntologyName)
                {
                    return AvailableOntologies[j].ID;
                }

            }
            return OntologyID;
        }
        public void RemoveFromOntology(OntList Ontology)
        {
            if (Ontology != null)
            OntList.Remove(Ontology);
        }

    }

    public class Biolit_Settings
    {
        static private string PrefFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Biolit_TechPreview_Dec_2009\BiolitSettings.xml");
        private static ConfigSettings settings = null;

        public static ConfigSettings Settings
        {
            get
            {
                if (settings == null)
                {
                    LoadSettings();
                }
                return settings;
            }
            set
            {
                settings = value;
            }

        }

        public static void LoadSettings()
        {
            try
            {
                XmlTextReader xtr = new XmlTextReader(PrefFileName);
                XmlSerializer deser = new XmlSerializer(typeof(ConfigSettings));
                settings = (ConfigSettings)deser.Deserialize(xtr);
                xtr.Close();
            }
            catch (Exception e)
            {
                try
                {
                    if (!File.Exists(PrefFileName + ".bak"))
                        File.Copy(PrefFileName, PrefFileName + ".bak");
                        settings = new ConfigSettings();
                }
                catch (Exception)
                {
                }
            }

            if (settings != null)
            {
                if (settings.DownloadURLLocations == null || settings.DownloadURLLocations.Length == 0)
                {
                    settings.DownloadURLLocations = new OntologyDownloadLocation[1];
                    settings.DownloadURLLocations[0] = new OntologyDownloadLocation("National Center for Biomedical Ontology", "http://rest.bioontology.org/bioportal/ontologies/download");
                    settings.SelectedDownloadURL = settings.DownloadURLLocations[0];
                }
                settings.AutoStart = true;
                settings.NCBI = true;
                settings.ProteinDataBank = true;
                settings.UniProt = true;
                //settings.ontologiesUpdate = true;
                if (settings.SettingsVersion == 0)
                {
                    settings.SettingsVersion = 1.0;
                    settings.OBO_DWNLDS = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Biolit_TechPreview_Dec_2009\Ontologies\");
                }
                else
                {
                    // in case some version specific work with the settigns needs to be done
                }
            }
        }


        public static void StoreSettings()
        {
            if (Settings != null)
            {
                settings.SettingsVersion = settings.CurrentSettingsVersion;

                XmlTextWriter xtw = new XmlTextWriter(PrefFileName, null);
                XmlSerializer ser = new XmlSerializer(typeof(ConfigSettings));
                ser.Serialize(xtw, (object)Settings);
                xtw.Close();
            }
        }
    }
}
